<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\business\Channel;
use app\api\business\NewsList;
use app\BaseController;
use app\common\lib\Show;
use app\Request;
use think\Exception;
use think\exception\HttpException;
use think\exception\ValidateException;

class Index extends BaseController
{
    private $channelIds = [];

    public function index()
    {
//        throw new HttpException(501,'数据库内部异常');
        throw new ValidateException(['msg' => '认证失败', 'code' => 405]);
//        echo $abc;
    }

    /**
     * 新闻频道
     * @return \think\response\Json
     */
    public function channelList()
    {
        $res = (new Channel())->getChannelList();
        // 数据为空
        if (empty($res)) {
            return Show::success([], '数据为空');
        }
        foreach ($res as $key => $val) {
            $this->channelIds[$key] = $val['id'];
        }
        // 数据正常返回
        return Show::success($res);
    }

    public function newsList(Request $request)
    {
        // 1、获取数据
        $channelId = $request->param('channelId', 1, 'intval');
        $page = $request->param('page', 1, 'intval');
        $limit = $request->param('limit', 2, 'intval');
        // 2、验证数据

        // 3、输出结果
        $res = (new NewsList())->getNews($channelId, $limit);
        if (empty($res)){
            return Show::success([]);
        }
        return Show::success($res);
    }
}
